<?php
class Admin extends Object
{
	function children()
	{
		global $db;
		$arr = NULL;
		if(is_numeric($this->AID))
		{
			$sql = "select * from Admin where PID={$this->AID}";
			$result = $db->query($sql);
			while($row = $db->fetch_array($result))
			{
				$arr[] = new Admin($row);
			}
		}
		return $arr;
	}
	function parent()
	{
		global $db;
		$admin = NULL;
		if($this->PID == 0)
			return $admin;
		$sql = "select * from Admin where AID={$this->PID}";
		$result = $db->query($sql);
		if($row = $db->fetch_array($result))
		{
			$admin = new admin($row);
		}
		return $admin;
	}
	function CPower()
	{
		global $db;
		$sql = "SELECT * FROM Mould";
		$result = $db->query($sql);
		while($row = $db->fetch_array($result))
		{
			$Moulds[$row['MID']] = $row;
		}
		$arrPower = unserialize($this->CPower);
		if($arrPower)
		{
			foreach ($arrPower as $key => $val)
			{
				$m = new Mould($Moulds[$arrPower[$key]['MID']]);
				//$m->getObjById($arrPower[$key]['MID'], 'MID', 'Mould');
				$arrPower[$key]['MUrl'] = $m->MUrl;
			}
		}
		//print_r($arrPower);
		return $arrPower;
	}
	function login($postdata)
	{
		extract($postdata);
		global $db;
		if($AName == '' || $APwd == ''){
			alert("用户名密码不为空");
		}
		
		$sql = "select * from Admin where AName='$AName'";
		$result = $db->query($sql);
		if($row = $db->fetch_array($result))
		{
			if($row['APwd'] == md5($APwd))
			{
				$_SESSION['AID'] = $row['AID'];
				$_SESSION['AName'] = $row['AName'];
				$_SESSION['Power'] = $row['Power'];
				$_SESSION['CPower'] = $row['CPower'];
				$_SESSION['APwd'] = $row['APwd'];//用于锁屏与解屏
				$_SESSION['MPower'] = $row['MPower'];//用于控制导航菜单
				//print_r($_SESSION);
				add_log($_SESSION['AName'].date("Y-m-d H:i:s").'登录系统','cms','','cms.wondershare.com','',$_SESSION['AName']);
				return true;
			}
		}
		return false;
	}
	function delete()
	{
		global $db;
		$sql = "delete * from Admin where AID={$this->AID}";
		$db->query($sql);
		$children = $this->children();
		if($children)
		{
			foreach($children as $child)
				$child->delete();
		}
		return true;
	}
}

/*
CREATE TABLE `Admin` (
  `AID` int(10) unsigned NOT NULL auto_increment,
  `AName` varchar(255) NOT NULL,
  `APwd` varchar(255) NOT NULL,
  `PID` int(10) unsigned NOT NULL,
  `AddDate` int(11) NOT NULL default '0',
  `Power` int(11) NOT NULL default '0',
  `CPower` text NOT NULL,
  PRIMARY KEY  (`AID`),
  UNIQUE KEY `AName` (`AName`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 
*/
?>